<?php

// @formatter:off
/**
 * @file ct.flows.update.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-06-03 03:40:32 +0000 (Tue, 03 Jun 2014) $
 *
 * $Id: ct.flows.update.php 112 2014-06-03 03:40:32Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/cron/ct.flows.update.php $
 */
// @formatter:on

class WCUpdateFlows extends WCCronTool {
	//
	// Constants.
	const MaxPerRun = 3000;
	//
	// Public methods.
	public function run($spacer = "") {
		//
		// Loading global pointers.
		global $wcOptions;

		$groupId = $wcOptions->activated("Group") ? $wcOptions->value("Group") : false;

		$done = false;
		$doneCount = 1;
		foreach(WCGroupsHolder::I()->items() as $group) {
			//
			// Ignoring not matching groups when there's a specified
			// one.
			if($groupId && $group->id != $groupId) {
				continue;
			}

			if(is_readable($group->root())) {
				echo "{$spacer}Checking group '{$group}':\n";

				$this->updateTagFlows($group, $done, $doneCount, "{$spacer}\t");
				$this->updateDirectoryFlows($group, $done, $doneCount, "{$spacer}\t");
				$this->updateCollectionFlows($group, $done, $doneCount, "{$spacer}\t");

				if($done) {
					break;
				}
			}
		}
	}
	public function showHelp($spacer = "") {
		echo "{$spacer}This tool seeks for any comic, collection, tag or directory that seems to have not a valid preview.\n";

		echo "\n";

		echo "{$spacer}Options:\n";
		echo "{$spacer}\t-g group-id, --group group-id\n";
		echo "{$spacer}\t\tIt only run active tags for an specific group.\n\n";
	}
	//
	// Protected methods.
	protected function loadOptions() {
		if(!$this->_optionsLoaded) {
			global $wcOptions;

			if($wcOptions->addValueOption("Group")) {
				$wcOptions->addOptionTrigger("Group", "g");
				$wcOptions->addOptionTrigger("Group", "group");
			}

			$wcOptions->check();

			$this->_optionsLoaded = true;
		}
	}
	protected function updateCollectionFlows(WCGroup &$group, &$done, &$doneCount, $spacer = "") {
		if(!$done) {
			foreach(WCCollectionsHolder::I()->itemsByGroup($group) as $itemsContainer) {
				$increment = 0;

				if(!$itemsContainer->hasValidFlow()) {
					echo "{$spacer}Generating collection '{$itemsContainer}' flow: ";

					$itemsContainer->generateFlow();

					if($itemsContainer->hasValidFlow()) {
						echo WCColorText::ShellGreen("Done\n");
					} else {
						echo WCColorText::ShellRed("Failed\n");
					}

					$increment = 1;
				}

				$doneCount += $increment;

				$done = self::MaxPerRun < $doneCount;

				if($done) {
					break;
				}
			}
		}
	}
	protected function updateDirectoryFlows(WCGroup &$group, &$done, &$doneCount, $spacer = "") {
		if(!$done) {
			foreach(WCDirectoriesHolder::I()->itemsByGroup($group) as $itemsContainer) {
				$increment = 0;

				if(!$itemsContainer->hasValidFlow()) {
					echo "{$spacer}Generating directory '{$itemsContainer}' flow: ";

					$itemsContainer->generateFlow();

					if($itemsContainer->hasValidFlow()) {
						echo WCColorText::ShellGreen("Done\n");
					} else {
						echo WCColorText::ShellRed("Failed\n");
					}

					$increment = 1;
				}

				$doneCount += $increment;

				$done = self::MaxPerRun < $doneCount;

				if($done) {
					break;
				}
			}
		}
	}
	protected function updateTagFlows(WCGroup &$group, &$done, &$doneCount, $spacer = "") {
		if(!$done) {
			foreach(WCTagsHolder::I()->itemsByGroup($group) as $itemsContainer) {
				$increment = 0;

				if(!$itemsContainer->hasValidFlow()) {
					echo "{$spacer}Generating tag '{$itemsContainer}' flow: ";

					$itemsContainer->generateFlow();

					if($itemsContainer->hasValidFlow()) {
						echo WCColorText::ShellGreen("Done\n");
					} else {
						echo WCColorText::ShellRed("Failed\n");
					}

					$increment = 1;
				}

				$doneCount += $increment;

				$done = self::MaxPerRun < $doneCount;

				if($done) {
					break;
				}
			}
		}
	}
}
